package org.jeecg.modules.zonglzp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.zonglzp.entity.AfterOrderEntity;
import org.jeecg.modules.zonglzp.entity.TeamAfterEntity;
import org.jeecg.modules.zonglzp.entity.teamAfteronepersonEntity;
import org.jeecg.modules.zonglzp.entity.trackAfterEntity;
import org.jeecg.modules.zonglzp.mapper.orderAfterMapper;
import org.jeecg.modules.zonglzp.service.orderAfterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class orderAfterServiceImpl implements orderAfterService {

    @Autowired
    orderAfterMapper mapper;

    /*
    查询售后
     */
    @Override
    public IPage<AfterOrderEntity> getlist(Page<AfterOrderEntity> page, String userId, int ordertype, String orderSn, String starttime, String endtime, String orgCode) {
        IPage<AfterOrderEntity> list = null;

        //如果ordertype是0  那就是面料
        if (ordertype == 0) {
            list = mapper.mianliaoshouhouList(page, userId, ordertype, orderSn, starttime, endtime,orgCode);
            for (int i = 0; i < list.getRecords().size(); i++) {
                String[] strArray = list.getRecords().get(i).getCartId().split(",");
                HashMap<String, Object> mianliaodetail = mapper.getfirstmianliao(strArray[0]);

                list.getRecords().get(i).setFabricCode(mianliaodetail.get("fabric_code").toString());
//                list.getRecords().get(i).setFabricPrice(mianliaodetail.get("fabric_price").toString());
                list.getRecords().get(i).setBuyNumber((Double) mianliaodetail.get("buy_number"));

                if( list.getRecords().get(i).getAfterPrice()==null){
                    list.getRecords().get(i).setAfterPrice("0");
                }
            }
        } else if (ordertype == 1) {
            //查询个订的
            list = mapper.gedingshouhoulist(page, userId, ordertype, orderSn, starttime, endtime,orgCode);
            for (int i = 0; i < list.getRecords().size(); i++){
                if( list.getRecords().get(i).getAfterPrice()==null){
                    list.getRecords().get(i).setAfterPrice("0");
                }
            }
        } else if (ordertype == 2) {
            list = mapper.tuandingshouhoulist(page, userId, ordertype, orderSn, starttime, endtime,orgCode);
            for (int i = 0; i < list.getRecords().size(); i++){
                if( list.getRecords().get(i).getAfterPrice()==null){
                    list.getRecords().get(i).setAfterPrice("0");
                }
            }
        }
        //IPage<AfterOrderEntity> list=mapper.getlist( page, userId,ordertype)
        return list;
    }


    //查询某个售后的详情
    @Override
    public AfterOrderEntity getorderatferdetail(int id) {
        AfterOrderEntity entity = mapper.getorderatferdetail(id);
        entity.setCreateTime(entity.getCreateTime().substring(0, 19));
        if (entity.getAfterSalesType() == 0) {
            entity.setAfterSalesString("退货");
        } else if (entity.getAfterSalesType() == 1) {
            entity.setAfterSalesString("换货");
        } else if (entity.getAfterSalesType() == 2) {
            entity.setAfterSalesString("返修");
        } else {
            entity.setAfterSalesString("重做");
        }
        return entity;
    }


    //查询某个售后订单的跟踪记录
    @Override
    public List<trackAfterEntity> getAftertrack(String afterOrderId) {

        List<trackAfterEntity> list = mapper.getAftertrack(afterOrderId);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getStatus() == 4) {
                list.get(i).setReceivingXs(true);
                HashMap<String, Object> yundan = mapper.getyundanbycaozuoId(list.get(i).getId());
                if (yundan != null) {
                    list.get(i).setKuaidiXs(true);
                    list.get(i).setReceivingXs(false);
                  //list.get(i).setFahuoTime(yundan.get("fahuoTime").toString());
                    list.get(i).setFahuoTime( list.get(i).getCreateTime());
                    //list.get(i).setCourierNumber(yundan.get("courier_number").toString());
                    list.get(i).setCourierNumber( list.get(i).getThirdSn());
                }
            } else if (list.get(i).getStatus() == 10 || list.get(i).getStatus() == 8) {
                //通过售后订单id查询售后类型
                int afterordertype=mapper.getafterordertypebyid(Integer.parseInt(afterOrderId));

                //用这个售后操作的id去查询这个操作产生的发货信息
                list.get(i).setKuaidiXs(true);
                if(list.get(i).getStatus() == 8&&afterordertype==2){
                    list.get(i).setKuaidiXs(false);
                }
                HashMap<String, Object> yundan = mapper.getyundanbycaozuoId(list.get(i).getId());

                if (yundan != null) {
                    //list.get(i).setFahuoTime(yundan.get("fahuoTime").toString());
                    list.get(i).setFahuoTime(list.get(i).getCreateTime());
                    //list.get(i).setCourierNumber(yundan.get("courier_number").toString());
                    list.get(i).setCourierNumber( list.get(i).getThirdSn());
                }
            }
        }
        return list;
    }


    /*
    这里发货操作
     */
    @Override
    public int fahuo(int courierId, String courierNumber, String aftertrackId, int afterId, int ordertype) {

       // 先查询该运单号是否已经插入快递表
//          String courierNumberold=mapper.getcunzaibycourierNumber(courierNumber);
//
//        //首先，往快递表里插入一条数据
//        if(courierNumberold==null){
//            mapper.addkuaidi(courierNumber,courierName,new Date());
//        }
//        // 然后查询插入的快递信息的ID 通过运单号
//          int kuaidiId=mapper.getkuaiIdbycourierNumber(courierNumber);
        //将快递id修改入售后订单跟踪表中
        mapper.updatetrackupdatxekuaidi(courierId, aftertrackId,courierNumber);
        //这个时候，再往订单售后跟踪表中再插入一条数据 确认发货的记录
        long id=System.currentTimeMillis();
        mapper.insertmlccTrackAfterOrder(afterId, ordertype, new Date(), 10, courierId, id, courierNumber);

        //如果是团订的 就要把人员也移过来
        if(ordertype==2){
            mapper.yidonggourpOrderPersonToNewTrack(aftertrackId,id);
        }
        return 1;
    }

    /*
    查询团订售后中 该订单需要进行售后的人员列表
     */
    @Override
    public IPage<AfterOrderEntity> getteamafterpeople(Page<TeamAfterEntity> page, int orderafterId) {
        return mapper.getteamafterpeople(page, orderafterId);
    }


    //查询团订售后的返修参数
    @Override
    public List<teamAfteronepersonEntity> getfanxiuParameter(String afterCode, String customerId, int isAfter, String code) {
        List<teamAfteronepersonEntity> list;
        if (isAfter == 1) {
            list = mapper.getfanxiuParameter(afterCode, customerId, isAfter);

        } else {

            //通过小产品编号查询小产品id
            String smallId=mapper.getsmallId(code);
            System.out.println("看看小产品id"+smallId);
            list = mapper.chongzuoParameter(afterCode, customerId, isAfter,smallId);
        }
        return list;
    }

    //获取这次团订售后这个人的返修原因和产品名字
    @Override
    public teamAfteronepersonEntity getReasonName(String afterCode, String customerId, String code) {
        return mapper.getReasonName(afterCode, customerId,code);
    }

    //获取个订售后中 返修的参数
    @Override
    public List<Map<String, Object>> getSingleOrdercanshu(int id, String productId) {
        return mapper.getSingleOrdercanshu(id,productId);
    }


    //通过个订售后id查询产品名字
    @Override
    public String getsingleafterproductName(int id) {
        return mapper.getsingleafterproductName(id);
    }


    //团订售后中 每次操作跟踪记录里的那批人
    @Override
    public IPage<TeamAfterEntity> getteamaftertrackpeoplegroup(Page<TeamAfterEntity> page, String id) {
        return mapper.getteamaftertrackpeoplegroup(page, id);
    }


    //查询
    @Override
    public Map<String, Object> getonetrackafterorderpersonpici(int id) {
        return mapper.getonetrackafterorderpersonpici(id);
    }

    //通过售后订单id查询面料编号
    @Override
    public String getFabricNumberbyafterId(int id) {
        return mapper.getFabricNumberbyafterId(id);
    }

    //通过token查询权限代码
    @Override
    public String getuserorgCode(String token) {
        return mapper.getuserorgCode(token);
    }


}
